﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using BLL;

namespace TaoBaoProject.MenbersHandler
{
  /// <summary>
  /// Summary description for AddMenbersHandler
  /// </summary>
  public class AddMenbersHandler : IHttpHandler
  {
    MenbersBLL menbersBLL = new MenbersBLL();
    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      try
      {
        string carFront = null;
        string carBack = null;
        string ph = null;
        Menbers menbers = new Menbers();
        menbers.QQ = context.Request["QQ"].ToString();
        menbers.Name = context.Request["Name"].ToString();

        int isSameQQAndName = menbersBLL.GetMenbers(menbers.QQ, menbers.Name);
        if (isSameQQAndName == 0)
        {
          SaveAllFiles(context.Request.Files, ref carFront, ref carBack, ref ph);
          menbers.IdentityCardAddress = context.Request["IdentityCardAddress"].ToString();
          menbers.IdentityCardFront = carFront != null ? carFront : " ";
          menbers.IdentityCardBack = carBack != null ? carBack : " ";
          menbers.ZhifubaoAccountName = context.Request["ZhifubaoAccountName"].ToString();
          menbers.Photo = ph != null ? ph : " ";
          menbersBLL.AddMenbers(menbers);
          string returnValue = "{QQ:'" + menbers.QQ + "',Name:'" + menbers.Name + "',IdentityCardAddress:'" + menbers.IdentityCardAddress + "',IdentityCardFront:'" + menbers.IdentityCardFront + "',IdentityCardBack:'" + menbers.IdentityCardBack + "',ZhifubaoAccountName:'" + menbers.ZhifubaoAccountName + "',Photo:'" + menbers.Photo + "'}";
          context.Response.Write(returnValue);
        }
        else
        {
          context.Response.Write("{Repeater:'1'}");
        }
      }
      catch (Exception ex)
      {

      }
    }

    private void SaveAllFiles(HttpFileCollection imgs, ref string carFront, ref string carBack, ref string ph)
    {
      try
      {
        if (imgs.Count > 0)
        {
          for (int i = 0; i < imgs.Count; i++)
          {
            if (imgs[i].ContentLength != 0)
            {
              string oldName = imgs[i].FileName.ToString();
              string imgName = Guid.NewGuid().ToString();
              string imgType = oldName.ToString().Substring(oldName.LastIndexOf("."));
              string imgPath = "~/UploadImgs/" + imgName + imgType;
              imgs[i].SaveAs(HttpContext.Current.Server.MapPath(imgPath));
              if (HttpContext.Current.Request.Files["IdentityCardFront"].FileName == imgs[i].FileName)
                carFront = imgName + imgType;
              if (HttpContext.Current.Request.Files["IdentityCardBack"].FileName == imgs[i].FileName)
                carBack = imgName + imgType;
              if (HttpContext.Current.Request.Files["Photo"].FileName == imgs[i].FileName)
                ph = imgName + imgType;
            }
          }
        }
      }
      catch (Exception ex)
      {

      }
    }


    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
}